<?php

class Database {
	private static $instance;
	private static $database;

        private $dbuser = 'root';        
        private $dbpass = 'root';
        private $dbstring = "mysql:host=localhost;dbname=lac_regalos";

/*
		private $dbuser = 'c1_lac_regalos';        
		private $dbpass = 'topotamadre';
		private $dbstring = "mysql:host=h1855107.stratoserver.net;dbname=c1_lac_regalos";
*/
        private $printerrors = true;

	private function __construct() {
		try {
                    self::$database = new PDO($this->dbstring, $this->dbuser, $this->dbpass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
                    if ($this->printerrors) {
                        self::$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    }
                    return self::$database;
		} catch (PDOException $e) {
                    if($this->printerrors){
			echo $e->getMessage();
                    }
		}
	}

        function __destruct() {
            self::$database = NULL;
        }


	private function __clone() {

	}

	public static function getInstance() {
		if(!(self::$instance instanceof self)) {
                    self::$instance = new self();
		}
		return self::$instance;
	}
        
        //pasarle la funciona mysql_special_chars a las q no hay params ???
        //INSERT,UPDATE
        public function exec($query) {
		try {
                    return self::$database->exec($query);;
		} catch (PDOException $e) {
                    if($this->printerrors){
			echo $e->getMessage();
                    }
		}
	}

        //select
        public function query($query){
            try {
                $db = self::$database->query($query);
                return $db->fetchAll(PDO::FETCH_ASSOC);
            } catch (PDOException $e) {
                if($this->printerrors){
                    echo $e->getMessage();
                }
            }
        }

        public function prepare($query,$params=null,$exec=true){
            try {
                $db = self::$database->prepare($query);
                if (!is_null($params)) {
                    foreach ($params as $key=>$p) {
                        if (htmlspecialchars_decode($params[$key], ENT_QUOTES) == $params[$key])
						{
                            $params[$key] = str_replace("'", "´", $params[$key]);
                            $params[$key] = str_replace("&#039;", "´",$params[$key]);
                            $params[$key] = str_replace("&", "and", $params[$key]);
                            $params[$key] = htmlspecialchars($params[$key], ENT_QUOTES, 'UTF-8');
                        }
                    }
                }
                $db->execute($params);
                if ($exec) {
                    return $db->fetchAll(PDO::FETCH_ASSOC);
                }
            } catch (PDOException $e) {
                if($this->printerrors){
                    echo $e->getMessage();
                }
            }
        }

        public function return_id(){
            return self::$database->lastInsertId();
        }
}
?>
